from django.shortcuts import render
from django.contrib.auth import authenticate, login,logout
from django.shortcuts import render, get_object_or_404
from django.shortcuts import get_list_or_404
from django.http import HttpResponse

from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.template.defaulttags import csrf_token

from django.template import RequestContext, loader
from django.views.decorators.csrf import csrf_exempt, csrf_protect

# Create your views here.

from django.template import RequestContext, loader
from django.views.decorators.csrf import csrf_exempt, csrf_protect
from django.contrib.auth.decorators import login_required
# Create your views here.

from tools import load_template
from tools import current_user
from tools import get_current_c_user
from tools import session_mixs

from user.forms import *
from isa.models import *
# Create your views here.
from tools import *


def login_view(request):
    print(request.method)
    if request.method == "POST":
        args = request.POST
        print(args)
        login_info = {
                'username':args['username'],
                'password':args['password'],
                }
        
        user = authenticate(**login_info)

        if user:
            if user.is_active:
                print("here ")
                
                try:
                    login(request,user)
                except ValueError as err :
                    return HttpResponseRedirect(reverse("isa:home"))
                
                name = user.username
                if not  name:
                    print("user is none")

                return HttpResponseRedirect(reverse("isa:home"))
            else:
                return HttpResponse("/")
        else:
            return HttpResponseRedirect(reverse("isa:home"))

    else:
        return HttpResponse("hello world")

@login_required
def user_out_view(request):
    logout(request)
    return HttpResponseRedirect("/")
